package com.haoran.grope;

import javax.annotation.PostConstruct;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StopWatch;

import com.haoran.grope.client.CalculatorClient;
import com.haoran.grope.client.RabbitMQClient;
import com.haoran.grope.client.Sender;

@SpringBootApplication
public class RabbitMQDirectApplication {

	public static void main(String[] args) {
		SpringApplication.run(RabbitMQDirectApplication.class, args);
	}

	@Autowired
	private RabbitMQClient client;

//	@Autowired
	private CalculatorClient calcClient;

	@Autowired
	private Sender sender;
	
	@PostConstruct
	public void init() {
		// director();
//		receave();
		sender();
	}

	private void sender() {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		for (int i = 0; i < 5; i++) {
			new Thread() {
				public void run() {
					sender.send("hi~!");		
				};
			}.start();
		}
		stopWatch.stop();
		System.out.println("总共耗时：" + stopWatch.getTotalTimeMillis());
	}

	private void director() {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		for (int i = 0; i < 100; i++) {
			client.send(new Sum(i, 1), "queue-sum");
		}
		stopWatch.stop();
		System.out.println("总共耗时：" + stopWatch.getTotalTimeMillis());
	}

	private void receave() {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		for (int i = 0; i < 5; i++) {
			// Object sum = calcClient.sum(i, 1);
			Object sum = calcClient.hello(i + "dssDDd");
			System.out.println(sum);
		}
		stopWatch.stop();
		System.out.println("总共耗时：" + stopWatch.getTotalTimeMillis());
	}

	@Bean
	public Queue queue() {
		return new Queue("queue-sum");
	}

	@Bean("amqp-sum")
	public Queue queue1() {
		return new Queue("amqp-sum");
	}

	@Bean("amqp-msg")
	public Queue queue2() {
		return new Queue("amqp-msg");
	}

	// 声明队列
	@Bean("queue3")
	public Queue queue3() {
		return new Queue("hello.queue1", true); // true表示持久化该队列
	}

	@Bean("queue4")
	public Queue queue4() {
		return new Queue("hello.queue2", true);
	}

	// 声明交互器
	@Bean 
	public TopicExchange topicExchange1() {
		return new TopicExchange("topic-exchange");
	}

	// 绑定
	@Bean
	public Binding binding1(@Qualifier("queue3") Queue queue1 ,TopicExchange topicExchange ) {
		return BindingBuilder.bind(queue1).to(topicExchange).with("key.1");
	}

	@Bean
	public Binding binding2(@Qualifier("queue4")Queue queue2 ,TopicExchange topicExchange) {
		return BindingBuilder.bind(queue2).to(topicExchange).with("key.#");
	}
}
